package com.fare.taluo.fetl.domain.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.fetl.domain.base.EntityFetlHibernateRepository;
import com.fare.taluo.fetl.domain.entity.DoublePlaneInfoEntity;
import com.fare.taluo.fetl.domain.repository.IDoublePlaneInfoRepository;
import com.fare.taluo.util.DateUtils;

@Repository("doublePlaneInfoRepository")
public class DoublePlaneInfoRepositoryImpl extends
		EntityFetlHibernateRepository implements IDoublePlaneInfoRepository {

	@Override
	public List<DoublePlaneInfoEntity> findPlaneInfoProperty(String hbaseKey,
			String fromCity, String toCity, String flightBatchDate,
			String returnFlightDate, String flightNo) {
		StringBuffer jpql =new StringBuffer("select plane from DoublePlaneInfoEntity plane where plane.grabDate>=? ");
		List<Object> conditionVals = new ArrayList<Object>();
		conditionVals.add(DateUtils.getCurrentDateStartTime());
		if(null!=hbaseKey && !"".equals(hbaseKey)){
			jpql.append(" and plane.attachHbaseKey =? ");
			conditionVals.add(hbaseKey);
		}
		if(null!=fromCity && !"".equals(fromCity)){
			jpql.append(" and plane.fromCity =? ");
			conditionVals.add(fromCity);
		}
		if(null!=toCity && !"".equals(toCity)){
			jpql.append(" and plane.toCity =? ");
			conditionVals.add(toCity);
		}
		if(null!=flightNo && !"".equals(flightNo)){
			jpql.append(" and plane.flightNo =? ");
			conditionVals.add(flightNo);
		}
		if(null!=flightBatchDate && !"".equals(flightBatchDate)){
			jpql.append(" and plane.flightDate =? ");
			conditionVals.add(flightBatchDate);
		}
		if(null!=returnFlightDate && !"".equals(returnFlightDate)){
			jpql.append(" and plane.returnFlightDate =? ");
			conditionVals.add(returnFlightDate);
		}
		
		List<DoublePlaneInfoEntity> list =null;
		Session session =getSessionFactory1().openSession();
		try {
			Query query = session.createQuery(jpql.toString());
			for (int i = 0; i < conditionVals.size(); i++) {
				query = query.setParameter(i, conditionVals.get(i));
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		return list;
	}

}
